;
(function() {
    var util = {
        /**
         * 获取url参数
         */
        getHashString: function(name, url, original) {
            if (!url) {
                url = window.location.href
            }

            var index = url.indexOf('#')
            if (index < 0) return null

            var paramStr = url.split('#')[1]

            var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
            var r = paramStr.match(reg)

            if (r !== null) {
                var ans = unescape(r[2])
                if (!original)
                    ans = decodeURIComponent(ans) // Url匹配完获取的值应为实际值

                return ans
            }
            return null
        },
        setHashString: function(name, value, url) {
            if (!url) {
                url = window.location.href
            }

            var temp = name + '=' + encodeURIComponent(value)
            if(value ==null){
                temp =  ""
            }

            var oldvalue = this.getHashString(name, url, true)
            if (oldvalue || oldvalue ==="") {
                if(temp ===""){
                    url = url.replace("&" +name + '=' + oldvalue, temp)
                    url = url.replace(name + '=' + oldvalue + "&", temp)
                }
                url = url.replace(name + '=' + oldvalue, temp)

                return url
            }


            var index = url.indexOf('#');
            if(temp){
                if (index < 0 && !temp){

                }else if (index < 0) {
                    url += '#' + temp
                } else {
                    var last = url.charAt(url.length-1);
                    if(last =='#'){
                        url += temp
                    }else{
                        url += '&' + temp
                    }
                }
            }

            return url;
        },
        setHashStringNew: function(name, value, url) {
            if (!url) {
                url = window.location.href
            }

            var temp = name + '=' + encodeURIComponent(value)
            if(value ==null){
                temp =  ""
            }

            url = url.split('#')[0];
            return url + '#' + temp;
        },
        getQueryString: function(name, url, original) {
            if (!url) {
                url = window.location.href
            }

            var index = url.indexOf('?')
            if (index < 0) return null

            var paramStr = url.split('?')[1]

            var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
            var r = paramStr.match(reg)

            if (r !== null) {
                var ans = unescape(r[2])
                if (!original)
                    ans = decodeURIComponent(ans) // Url匹配完获取的值应为实际值

                return ans.split('#')[0];
            }
            return null
        },

        setQueryString: function(name, value, url) {
            if (!url) {
                url = window.location.href
            }

            var temp = name + '=' + encodeURIComponent(value)
            if(value ==null){
                temp =  ""
            }

            var oldvalue = this.getHashString(name, url, true)
            if (oldvalue || oldvalue ==="") {
                if(temp ===""){
                    url = url.replace("&" +name + '=' + oldvalue, temp)
                    url = url.replace(name + '=' + oldvalue + "&", temp)
                }
                url = url.replace(name + '=' + oldvalue, temp)

                return url
            }


            var index = url.indexOf('?');
            if(temp){
                if (index < 0 && !temp){

                }else if (index < 0) {
                    url += '?' + temp
                } else {
                    var last = url.charAt(url.length-1);
                    if(last =='?'){
                        url += temp
                    }else{
                        url += '&' + temp
                    }
                }
            }

            return url;
        },
        getRequest: function() {
            var url = window.location.search;
            if (!url) {
                return null;
            }
            var theRequest = new Object();
            if (url.indexOf("?") != -1) {
                var str = url.substr(1);
                strs = str.split("&");
                for (var i = 0; i < strs.length; i++) {
                    theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
                }
            }
            return theRequest;
        },
        getNewRequestUrl: function(options) {
            var url = window.location.href.split("?")[0];
            if (!url) {
                return null;
            }
            if (options.length == 0) {
                return url;
            }
            url += '?';
            for (var i = 0; i < options.length; i++) {
                url += options[i].key + '=' + options[i].val;
                if (i < options.length - 1) {
                    url += '&';
                }
            }
            return url;
        },
        isMobile: function() {
            var ua = navigator.userAgent.toLowerCase();
            if (ua.match(/iphone|ipod|ios|android|nokia|Windows Phone OS|blackberry|webos|MeeGo|smartphone|symbian|s60|Series60|ucweb|uc brower|opera mini|opera mobi|mini 9.5|320x320|240x320|176x220|320*480|480*320|240*320|320*320|176*220|vx1000|lge|m800|e860|u940|ux840|compal|wireless|mobi|ahong|lg380|lgku|lgu900|lg210|lg47|lg920|lg840|lg370|sam-r|mg50|s55|g83|t66|vx400|mk99|d615|d763|el370|sl900|mp500|samu3|samu4|vx10|xda_|samu5|samu6|samu7|samu9|a615|b832|m881|s920|n210|s700|c-810|_h797|mob-x|sk16d|848b|mowser|s580|r800|471x|v120|rim8|c500foma:|160x|x160|480x|x640|t503|w839|i250|sprint|w398samr810|m5252|c7100|mt126|x225|s5330|s820|htil-g1|fly v71|s302|-x113|novarra|k610i|-three|8325rc|8352rc|sanyo|vx54|c888|nx250|n120|mtk |c5588|s710|t880|c5005|i;458x|p404i|s210|c5100|teleca|s940|c500|s590|foma|samsu|vx8|vx9|a1000|_mms|myx|a700|gu1100|bc831|e300|ems100|me701|me702m-three|sd588|s800|8325rc|ac831|mw200|brew |d88|htc\/|htc_touch|355x|m50|km100|d736|p-9521|telco|sl74|ktouch|m4u\/|me702|8325rc|kddi|phone|lg |sonyericsson|samsung|240x|x320|vx10|nokia|sony cmd|motorola|up.browser|up.link|mmp|symbian|smartphone|midp|wap|vodafone|o2|pocket|mobile|psp|treo/i)) {
                return true;
            } 

            return false;
        },
        getMobileBrowser: function() {
            var ua = navigator.userAgent;

            if(ua.indexOf('Firefox') > -1 || ua.indexOf('FxiOS') > -1){
                return 'Firefox';
            }

            if (ua.indexOf('Opera') > -1) {
                return 'Opera';
            }

            if (ua.indexOf('UCBrowser') > -1) {
                return 'UCBrowser';
            }

            if (ua.indexOf('MicroMessenger') > -1) {
                return 'MicroMessenger'; // 微信内置浏览器
            }

            if (ua.indexOf('MQQBrowser') > -1) {
                return 'MQQBrowser'; // QQ浏览器
            }

            if (ua.match(/QQ/i) == 'QQ') {
                return 'QQ'; // QQ内置浏览器
            }

            if(ua.indexOf('Chrome') > -1 || ua.indexOf('CriOS') > -1){
                return 'Chrome';
            }

            if (ua.indexOf('Safari') > -1) {
                return 'Safari';
            }

            return '';
        },
    };

    window.util = util;
})();